package pacman.genetic;
/******************************************************************************
*  A Teaching GA					  Developed by Hal Stringer & Annie Wu, UCF
*  Version 2, January 18, 2004
*******************************************************************************/

import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;

import com.sun.org.apache.xalan.internal.xsltc.runtime.Parameter;

import pacman.Executor;
import pacman.controllers.examples.RandomGhosts;
import pacman.controllers.examples.StarterGhosts;
import pacman.controllers.examples.AggressiveGhosts;
import pacman.entries.pacman.HRTPacMan;

public class GAPacman extends FitnessFunction{

/*******************************************************************************
*                            INSTANCE VARIABLES                                *
*******************************************************************************/


/*******************************************************************************
*                            STATIC VARIABLES                                  *
*******************************************************************************/


/*******************************************************************************
*                              CONSTRUCTORS                                    *
*******************************************************************************/

	public GAPacman(){
		name = "OneMax Problem";
	}

/*******************************************************************************
*                                MEMBER METHODS                                *
*******************************************************************************/

//  COMPUTE A CHROMOSOME'S RAW FITNESS *************************************

	public void doRawFitness(Chromo X){

		
		Executor exec=new Executor();
		int[] coefficient = new int[4];
		X.rawFitness = 0;
		
		List<String> list = splitEqually(X.chromo, 8);
		coefficient[0] = Integer.parseInt(list.get(0),2);
		coefficient[1] = Integer.parseInt(list.get(1),2);
		coefficient[2] = Integer.parseInt(list.get(2),2);
		coefficient[3] = Integer.parseInt(list.get(3),2);
		//run multiple games in batch mode - good for testing.
		int numTrials=1;
		X.fitnesses = new double[3];
		for(int i = 0; i < X.fitnesses.length; i++)
			X.fitnesses[i] = 0;
		switch(Parameters.ghostType){
			case 1:
				X.fitnesses[0] = exec.runExperiment(new HRTPacMan(),new RandomGhosts(), numTrials, coefficient);
				X.fitnesses[1] = exec.runExperiment(new HRTPacMan(),new StarterGhosts(), numTrials, coefficient);
				X.fitnesses[2] = exec.runExperiment(new HRTPacMan(),new AggressiveGhosts(), numTrials, coefficient);
	    		
	    		X.rawFitness = 0;
	    		for(int i = 0; i < X.fitnesses.length; i++)
	    			X.rawFitness += X.fitnesses[i];
	    		X.rawFitness /= X.fitnesses.length;                 
	    		break;
			case 2:
	    		X.rawFitness = exec.runExperiment(new HRTPacMan(),new RandomGhosts(), numTrials, coefficient);
	    		break;
			case 3:
	    		X.rawFitness = exec.runExperiment(new HRTPacMan(),new StarterGhosts(), numTrials, coefficient);
	    		break;
			case 4:
	    		X.rawFitness = exec.runExperiment(new HRTPacMan(),new AggressiveGhosts(), numTrials, coefficient);
	    		break;
			default:
	    		X.rawFitness = exec.runExperiment(new HRTPacMan(),new RandomGhosts(), numTrials, coefficient);
	    		break;		
		}
		

//		System.out.println(X.rawFitness);
		
	}

//  PRINT OUT AN INDIVIDUAL GENE TO THE SUMMARY FILE *********************************

	public void doPrintGenes(Chromo X, FileWriter output) throws java.io.IOException{

		for (int i=0; i<Parameters.numGenes; i++){
			output.write("#" + X.chromo);
			//Hwrite.right(X.getGeneAlpha(i),11,output);
		}
		output.write("\n        ");
		output.write("#   RawFitness: ");
		for (int i=0; i<Parameters.numGenes; i++){
			Hwrite.right(X.getPosIntGeneValue(i),11,output);
		}
		Hwrite.right((int) X.rawFitness,13,output);
		output.write("\n\n");
		return;
	}
	
	public static List<String> splitEqually(String text, int size) {
	    // Give the list the right capacity to start with. You could use an array
	    // instead if you wanted.
	    List<String> ret = new ArrayList<String>((text.length() + size - 1) / size);

	    for (int start = 0; start < text.length(); start += size) {
	        ret.add(text.substring(start, Math.min(text.length(), start + size)));
	    }
	    return ret;
	}
	

/*******************************************************************************
*                             STATIC METHODS                                   *
*******************************************************************************/

}   // End of OneMax.java ******************************************************

